// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import {
  fetch,
  post
} from './util/http'
import '../src/util/htmlRem'
import '../src/styles/reset.css'
import '../src/styles/common.css'
import './weui/weui.css'
import './weui/example.css'

import {
  ToastPlugin
} from 'vux';
Vue.use(ToastPlugin);

import {
  AlertPlugin
} from 'vux';
Vue.use(AlertPlugin);

import {
  LoadingPlugin
} from 'vux';
Vue.use(LoadingPlugin);

import {
  ConfirmPlugin
} from 'vux';
Vue.use(ConfirmPlugin)

import NavTitle from './components/NavTitle';
Vue.use(NavTitle);

import Uploader from './components/uploader/index';
Vue.use(Uploader);

import ShowAnnex from './components/ShowAnnex';
Vue.use(ShowAnnex);

import {
  dateFormat
} from 'vux';

var format = function(time, format) {

  var tf = function(i) {
    return (i < 10 ? '0' : '') + i
  };
  return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) {
    switch (a) {
      case 'yyyy':
        return tf(t.getFullYear());
        break;
      case 'MM':
        return tf(t.getMonth() + 1);
        break;
      case 'mm':
        return tf(t.getMinutes());
        break;
      case 'dd':
        return tf(t.getDate());
        break;
      case 'HH':
        return tf(t.getHours());
        break;
      case 'ss':
        return tf(t.getSeconds());
        break;
    }
  })
}

Vue.filter('dateFormat', function(value) {
  if (value && value != null) {
    // 目标时区，东8区
    let targetTimezone = -8
    // 当前时区与中时区时差，以min为维度
    let _dif = new Date().getTimezoneOffset()
    // 本地时区时间 + 时差  = 中时区时间
    // 目标时区时间 + 时差 = 中时区时间
    // 目标时区时间 = 本地时区时间 + 本地时区时差 - 目标时区时差
    // 东9区时间
    let east9time = new Date(value).getTime() + _dif * 60 * 1000 - (targetTimezone * 60 * 60 * 1000)
    return dateFormat(new Date(east9time), 'YYYY-MM-DD');
  } else {
    return " ";
  }

})
Vue.filter('dateFormat2', function(value) {
  if (value && value != null) {
    // 目标时区，东8区
    let targetTimezone = -8
    // 当前时区与中时区时差，以min为维度
    let _dif = new Date().getTimezoneOffset()
    // 本地时区时间 + 时差  = 中时区时间
    // 目标时区时间 + 时差 = 中时区时间
    // 目标时区时间 = 本地时区时间 + 本地时区时差 - 目标时区时差
    // 东9区时间
    let east9time = new Date(value).getTime() + _dif * 60 * 1000 - (targetTimezone * 60 * 60 * 1000)
    return dateFormat(new Date(east9time), 'YYYY-MM-DD HH:mm:ss');
  } else {
    return " ";
  }

})

Vue.config.productionTip = false;


Vue.prototype.$post = post;
Vue.prototype.$get = fetch;

router.beforeEach(function(to, from, next) {
  store.commit('updateLoadingStatus', {
    isLoading: true
  });
  if (to.matched.length === 0) {
    next('/warn');
    store.commit('updateLoadingStatus', {
      isLoading: false
    });
  } else {
    //必须要加next()
    next();
  }
});

router.afterEach(function(to) {
  window.scrollTo(0, 1);
  store.commit('updateLoadingStatus', {
    isLoading: false
  });
});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: {
    App
  }
})
